@extends('admin.layouts.admin')
@section('header') <link rel="stylesheet" href="{{ asset('admin/style/admin.css') }}" media="all"> @endsection
@section('content')
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline lay-search">
                            <input type="text" name="title" placeholder="分类昵称" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        @include('admin.layouts._layout.form_search')
                    </div>
                </div>
            </div>

            <div class="layui-card-body">
                <div style="padding-bottom: 10px;">
                    {{--<button class="layui-btn layuiadmin-btn-search" data-type="batchdel">批量删除</button>--}}
                    <a href="javascript:;" layadmin-event="back" class="layui-btn layui-btn-primary">返回上级</a>
                    <button class="layui-btn layuiadmin-btn-search" data-type="add">添加配置</button>
                </div>

                <table id="LAY-form" lay-filter="LAY-form"></table>

                <input type="hidden" id="setting_config_tab_id" value="{{$request->setting_config_tab_id}}" name="">
                <script type="text/html" id="tpl_title">
                    <span><a data-name="@{{d.title}}" href="{{route('admin.setting_config.index')}}">@{{d.title}}</a></span>
                </script>
                <script type="text/html" id="tpl_status">
                    <input type="checkbox" name="status" lay-skin="switch" value="@{{d.id}}"  lay-text="是|否" lay-filter="lockDemo" @{{ d.status == 1 ? 'checked' : '' }}>
                </script>

                <script type="text/html" id="table-controller">
                    @include('admin.layouts._layout.form_button')
                </script>
            </div>
        </div>
    </div>
    <input type="hidden" id="select_type">
    <form id="destroy_form" method="post">{{ csrf_field() }} {{ method_field('DELETE') }}</form>
@endsection
@section('scripts')
    <script>
        layui.config({
            base: '../' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form'], function(){
            var $ = layui.$
                ,form = layui.form
                ,table = layui.table
                ,views = layui.view;

            var setting_config_tab_id = $('#setting_config_tab_id').val()
            /**
             * 表格列表
             */
            var cols = [[
                // {type: "checkbox", fixed: "left"},
                {type:  "numbers", title: "ID"}
                ,{field: "id", title: "分类ID"}
                ,{field: "info", title: "配置名称"}
                ,{field: "menu_name", title: "字段变量"}
                ,{field: "type", title: "字段类型"}
                ,{field: "status", title: "是否显示",templet:"#tpl_status"}
                ,{field: "value", title: "值"}
                ,{field: "created_at", title: "创建时间", sort : !0}
                ,{title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-controller'}]];
            var where = {'setting_config_tab_id' : setting_config_tab_id}
            var initTable = Common.initTable(cols, where);

            /**
             * 监听搜索
             */
            form.on('submit(LAY-search)', function(data){
                Common.search(data,1);
            });

            /**
             * 监听重置
             */
            form.on('submit(LAY-reset)', function(data){
                $('.lay-search input').val('');
                $(".layui-form select").find("option:first").prop("selected", true);
                form.render();
                Common.search(data,2);
            });

            /**
             * 监听锁定操作
             */
            form.on('switch(lockDemo)', function(obj){
                // layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
                var url = "setStatus/" + this.value;
                Common.setStatus(url)
            });
            /**
             * 监听排序
             */
            table.on('sort(LAY-form)', function(obj){
                Common.sort(obj);
            });

            /**
             * 监听单元格编辑
             **/
            table.on('edit(LAY-form)', function(obj){
                var postUrl = "/admin/setting_config_tab/update/" + obj.data.id
                Common.editCols(postUrl,obj);
            });

            /**
             * 操作监听 删除和修改
             * @controllers del edit
             */
            table.on("tool(LAY-form)", function(e) {
                if ("del" === e.event) {
                    var postUrl = "/admin/setting_config/destroy/"+e.data.id;
                    Common.deleteTable(postUrl,e);
                }else if ("edit" === e.event) {
                    var type = 2;
                    var title = '编辑';
                    var modalUrl = "/admin/setting_config/edit/" + e.data.id;
                    var postUrl = "/admin/setting_config/update/" + e.data.id;
                    var width = '600px';
                    var height = '700px';
                    Common.editTable(type, title, modalUrl, postUrl, width, height);
                }
            });

            /**
             * 事件 添加和批量删除
             * @controllers {batchdel: batchdel, add: add}
            */
            $('.layui-btn.layuiadmin-btn-search').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            var active = {
                batchdel: function(){
                    //删除包含子栏目，以及对应的文章，所以不进行批量删除
                    return layer.msg('无法批量删除', {
                        icon: 2
                        ,time: 1000
                    });
                }
                ,add: function(){
                    layer.open({
                        type: 2,
                        title: "选择添加类型",
                        content: "{{ route('admin.setting_config.select_type') }}",
                        maxmin: true,
                        area: ["1000px", "200px"],
                        btn: ["提交", "取消"],
                        yes: function(index, layero) {
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'LAY-select-forms'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID)
                                ,input_type = '';
                            iframeWindow.layui.form.on('submit('+submitID+')', function(data){
                                input_type = data.field.input_type;
                                $('#select_type').val(input_type);
                            })
                            input_type = $('#select_type').val();
                            submit.trigger('click');//模拟点击
                            layer.close(index);
                            var type = 2;
                            var title = '添加';
                            var modalUrl = "{{ route('admin.setting_config.create') }}?setting_config_tab_id="+setting_config_tab_id+'&input_type='+input_type;
                            var postUrl = "{{ route('admin.setting_config.store') }}";
                            var width = '600px';
                            var height = '700px';
                            Common.addTable(title, modalUrl, postUrl, width, height);
                        }
                    })
                    return false;

                }
            };
        });//use

    </script>
@stop
